// pages/expressOrder/expressOrder.js
Page({

      /**
       * 页面的初始数据
       */
      data: {
            orderList:[
            ],
            status:-1
      },
      /**选择分类 */
      selectStatus(){
            // let id=e.currentTarget.dataset.id;
            let index;
            wx.showActionSheet({
                  itemList: ['全部', '正在进行','已完成'],
                  success:res=>{
                        if(res.tapIndex==0){
                              this.setData({
                                    status:-1
                              },()=>{
                                    this.initOrder()
                              })
                        }else if(res.tapIndex==1){
                              this.setData({
                                    status:1
                              },()=>{
                                    this.initOrder()
                              })
                        }
                        else if(res.tapIndex==2){
                              this.setData({
                                    status:2
                              },()=>{
                                    this.initOrder()
                              })
                        }
                  },
                  fail (res) {
                        console.log(res.errMsg)
                  }
            })
      },
      selectHeader(e){
            if(e.currentTarget.dataset.status==this.data.status){
                  return;
            }
            this.setData({
                  status:e.currentTarget.dataset.status
            },function () {
                  this.initOrder()
            })
      },
      onPhone(){
            wx.makePhoneCall({
                  phoneNumber: getApp().globalData.phone,
            })
      },
      onConfirm(e){
            wx.showLoading({
              title: '确认收货',
            })
            // 确认收货
            wx.request({
                  url: getApp().globalData.host+'/client/user/setExpressOrderStatus',
                  header: {
                        'content-type': 'application/x-www-form-urlencoded'
                  },
                  method:'PUT',
                  data:{
                        id:e.currentTarget.dataset.id,
                        status:2,
                        currentStatus:this.data.status,
                        userId:wx.getStorageSync('userId')
                  },
                  success:res=>{
                        this.formatData(res.data.data.orderList);
                  },
                  fail:()=>{
                        wx.showToast({
                          title: '服务器繁忙',
                          icon:'error'
                        })
                  },
                  complete:()=>{
                        wx.hideLoading()
                  }
            })
      },

      /**
       * 生命周期函数--监听页面加载
       */
      onLoad: function (options) {

      },

      /**
       * 生命周期函数--监听页面显示
       */
      onShow: function () {
            this.initOrder()
      },
      initOrder(){
            wx.showLoading({
                  title: '加载订单',
                })
                let userId=wx.getStorageSync('userId');
    
                wx.request({
                      url: getApp().globalData.host+'/client/user/orderList',
                      data:{
                            userId:userId,
                            status:this.data.status
                      },
                      success:res=>{
                            this.formatData(res.data.data.orderList);
                      },
                      fail:()=>{
                            wx.showToast({
                              title: '服务器繁忙',
                              icon:'error'
                            })
                      },
                      complete:()=>{
                            wx.hideLoading()
                      }
                })
      },

      formatData(e){
            e.forEach(item=>{
                  item.createTime=this.formatTime(item.createTime);
            })
            this.setData({
                  orderList:e
            })
      },
      formatTime(e){
            var time =  new Date(e);
            var y = time.getFullYear();
            var m = time.getMonth()+1;
            var d = time.getDate();
            let t= y+'年'+m+'月'+d+'日'+' '+time.getHours()+'时'+time.getMinutes()+'分'+time.getSeconds()+'秒';
            return t;
      }
})